//mongoutils数据库工具
var client = require('mongodb').MongoClient

function Mongoutil() {
	this.url = 'mongodb://localhost:27017/mingo';
}

Mongoutil.prototype.connect = function (callback) {
	var that = this;
	client.connect(this.url, function (err, db) {
		if (err) {
			console.dir(err);
		} else {
			that.db = db;
			callback();
		}
	});
}

Mongoutil.prototype.close = function () {
	this.db.close();
}

Mongoutil.prototype.insertDocuments = function (collectionName, docs, callback) {
	var that = this;
	this.connect(function () {
		var collection = that.db.collection(collectionName);
		collection.insertMany(docs, function (err, result) {
			if (err) {
				console.dir(err);
			} else {
				callback(result);
			}
			that.close();
		});
	});
}

Mongoutil.prototype.insertDocument = function (collectionName, doc, callback) {
	var that = this;
	this.connect(function () {
		var collection = that.db.collection(collectionName);
		collection.insertOne(doc, function (err, result) {
			if (err) {
				console.dir(err);
			} else {
				callback && callback(result.insertedCount);
			}
			that.close();
		});
	});
}

Mongoutil.prototype.findAllDocuments = function (collectionName, sort, limit, callback) {
	var that = this;
	this.connect(function () {
		var collection = that.db.collection(collectionName);
		collection.find({}).sort(sort).limit(limit).toArray(function (err, result) {
			if (err) {
				console.dir(err);
			} else {
				callback && callback(result);
			}
			that.close();
		});
	});
}

Mongoutil.prototype.findOne = function (collectionName, query, callback) {
	var that = this;
	this.connect(function () {
		var collection = that.db.collection(collectionName);
		collection.findOne(query)//then():ES6中的方法，相当于回调函数
			.then(function (doc) {//doc:是查找到的结果
				callback(doc);
				that.close()
			});
	});
}
Mongoutil.prototype.findOneAndUpdate = function (collectionName, filter, update, callback) {
	var that = this;
	this.connect(function () {
		var collection = that.db.collection(collectionName);
		collection.findOneAndUpdate(filter, update)//then():ES6中的方法，相当于回调函数
			.then(function (doc) {//doc:是查找到的结果
				callback(doc);
				that.close()
			});
	});
}

Mongoutil.prototype.findAllDocument = function (collectionName, query, callback) {
	var that = this;
	this.connect(function () {
		var collection = that.db.collection(collectionName);
		collection.find(query).toArray(function (err, result) {
			if (err) {
				console.dir(err);
			} else {
				callback(result);
			}
			that.close();
		});
	});
}

Mongoutil.prototype.update = function (collectionName, filter, update, callback) {
	var that = this;
	this.connect(function () {
		var collection = that.db.collection(collectionName);
		collection.updateOne(filter, update, function (err, result) {
			if (err) {
				console.dir(err);
			} else {
				callback(result.result.nModified);
			}
		});
	});
}

Mongoutil.prototype.deleteOne = function (collectionName, filter, callback) {
	var that = this;
	this.connect(function () {
		var collection = that.db.collection(collectionName);
		collection.deleteOne(filter, function (err, result) {
			if (err) {
				console.dir(err);
			} else {
				callback(result.result.nModified);
			}
		});
	});
}

Mongoutil.prototype.deleteAll = function (collectionName, filter, callback) {
	var that = this;
	this.connect(function () {
		var collection = that.db.collection(collectionName);
		collection.deleteAll(filter, function (err, result) {
			if (err) {
				console.dir(err);
			} else {
				callback(result.result.nModified);
			}
		});
	});
}
module.exports = new Mongoutil();